const app = getApp();

Page({
    data: {
        product: null,
        reasonText: '',
        description: '',
        showReasonPicker: false,
        submitting: false,
        reasonOptions: [
            { name: '虚假信息' },
            { name: '违禁物品' },
            { name: '不当行为' },
            { name: '骚扰行为' },
            { name: '欺诈行为' },
            { name: '其他' }
        ]
    },

    onLoad(options) {
        const { productId } = options;
        if (productId) {
            this.loadProductDetail(productId);
        }
    },

    // 加载商品详情
    async loadProductDetail(productId) {
        try {
            const res = await new Promise((resolve, reject) => {
                wx.request({
                    url: `${app.globalData.baseUrl}/api/products/${productId}`,
                    method: 'GET',
                    success: resolve,
                    fail: reject
                });
            });

            if (res.statusCode === 200 && res.data.success) {
                this.setData({
                    product: res.data.data.product
                });
            } else {
                throw new Error(res.data.message || '获取商品信息失败');
            }
        } catch (error) {
            console.error('获取商品信息失败：', error);
            wx.showToast({
                title: '获取商品信息失败',
                icon: 'none'
            });
        }
    },

    // 显示举报原因选择器
    showReasonPicker() {
        this.setData({ showReasonPicker: true });
    },

    // 关闭举报原因选择器
    onReasonPickerClose() {
        this.setData({ showReasonPicker: false });
    },

    // 选择举报原因
    onReasonSelect(event) {
        const { name } = event.detail;
        this.setData({
            reasonText: name,
            showReasonPicker: false
        });
    },

    // 详细说明变化
    onDescriptionChange(event) {
        this.setData({
            description: event.detail
        });
    },

    // 提交举报
    async submitReport() {
        if (!this.data.reasonText) {
            wx.showToast({
                title: '请选择举报原因',
                icon: 'none'
            });
            return;
        }

        if (this.data.description.length < 10) {
            wx.showToast({
                title: '详细说明至少需要10个字',
                icon: 'none'
            });
            return;
        }

        this.setData({ submitting: true });

        try {
            const res = await new Promise((resolve, reject) => {
                wx.request({
                    url: `${app.globalData.baseUrl}/api/reports`,
                    method: 'POST',
                    header: {
                        'Authorization': `Bearer ${wx.getStorageSync('token')}`,
                        'Content-Type': 'application/json'
                    },
                    data: {
                        type: 'product',
                        targetId: this.data.product._id,
                        reason: this.data.reasonText,
                        description: this.data.description
                    },
                    success: resolve,
                    fail: reject
                });
            });

            if (res.statusCode === 200 && res.success) {
                wx.showToast({
                    title: '举报提交成功',
                    icon: 'success'
                });

                // 延迟返回上一页
                setTimeout(() => {
                    wx.navigateBack();
                }, 1500);
            } else {
                throw new Error(res.message || '提交举报失败');
            }
        } catch (error) {
            console.error('提交举报失败：', error);
            wx.showToast({
                title: '提交举报失败',
                icon: 'none'
            });
        } finally {
            this.setData({ submitting: false });
        }
    }
}); 